﻿using System;
using System.Collections.Generic;
using System.Linq;

using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain.Special;
using Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Mappers;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess
{
    internal class CmsUserGetInformationDataCommand : CmsUserGetInformationCommand
    {
        public override CmsUserInformation ExecuteOne()
        {
            CmsUserInformation result = null;

            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsUserInformation>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsUserInformation>;

            if (currentUnitOfWork != null)
            {
                result = SqlUtility<CmsUserInformation, int>.ExecuteMultiReader(
                    currentUnitOfWork.Connection,
                    currentUnitOfWork.Transaction,
                    "cms_sp_GetUserAndUserGroups",
                    new
                    {
                        LoginName = CommandParameter.LoginName,
                        SiteId = CommandParameter.SiteId,
                        SiteGroupId = CommandParameter.SiteGroupId
                    },
                    new CmsUserInformationAllForManagementDataMapper());
            }

            return result;
        }
    }
}
